//
//  QCouponDetailVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/27.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QCouponDetailVC.h"
#import "QCouponTwoCell.h"
#import "QMyCouponCell.h"
#import "QCouponModel.h"
@interface QCouponDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView * _bgView;
    NSMutableArray * _dataArr;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QCouponModel * model;
@end

@implementation QCouponDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"优惠券详情";
    _dataArr = [NSMutableArray arrayWithObjects:@"使用说明", nil];
    [self createUI];
    [self requestData];
}

- (void)requestData{
    WS(wSelf);
    NSString *url = [QParamService urlWithAction:@"getCouponValue"];
        NSDictionary *param = [QParamService getCouponValueWithCardCode:self.cardCode];
        [QViewUtility showMBHudToView:self.view hideBg:YES];
        [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
            [QViewUtility hideMBHudToView:self.view];
            if(error){
                return ;
            }
            QBaseModel *basModel = [QBaseModel objectFromDict:dict];
            if([basModel.result isEqualToString:@"success"]){
               
                _model = [QCouponModel objectFromDict:basModel.items];
                NSArray * arr = [_model.notice componentsSeparatedByString:@";"];
                NSLog(@"=========%@",_model.areaShow);
                //[_dataArr setObject:str atIndexedSubscript:0];
                [_dataArr addObjectsFromArray:arr];
                
                [wSelf.tbView reloadData];
            }else{
                
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }
        }];
        

}

- (void)createUI{
    _bgView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 130, kScreenWidth - 30, kScreenHeight - 145)];

    _bgView.image = [UIImage imageNamed:@"Coupon_bgView"];
    
    [self.view addSubview:_bgView];

    [self.view addSubview:self.tbView];
    self.tbView.frame = CGRectMake(15, 15 + kNavHeight+kStatusHeight, kScreenWidth - 30, kScreenHeight);

    [self.tbView registerNib:[UINib nibWithNibName:@"QMyCouponCell" bundle:nil] forCellReuseIdentifier:@"QMyCouponCell"];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QCouponTwoCell" bundle:nil] forCellReuseIdentifier:@"QCouponTwoCell"];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if(offsetY > 0){
        _bgView.frame = CGRectMake(15, -offsetY+130, kScreenWidth - 30, kScreenHeight - 145);
    }else{
        _bgView.frame = CGRectMake(15, 130-offsetY, kScreenWidth - 30, kScreenHeight - 145);
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 10;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 122;
    }else{
        return 20;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return _dataArr.count ;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        QMyCouponCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QMyCouponCell"];
        if (_model != nil) {
            cell.model = _model;
        }
        return cell;
    }else{
        QCouponTwoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QCouponTwoCell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.noticeStr = _dataArr [indexPath.row];
        cell.indexPath = indexPath;
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
